package com.jader.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jader.beans.User;
import com.jader.fragment.SouYeFragment;
import com.jader.myviews.MyProgressDialog;
import com.jader.util.JaderApplication;
import com.jader.util.MD5Tool;
import com.jader.util.PreferenceUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class LoginActivity extends Activity implements OnClickListener {
	private EditText edit_phone, edit_password;
	private Button login;
	private TextView tv_forget, tv_register, tv_login_title;
	private ImageView login_back;
	private String login_phone = "";
	private String login_password = "";
	private String encode = "";
	private HttpUtils httpUtils;
	String url = JaderApplication.getIP() + "/Jader/client";
	private MyProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initView();
		viewLinstener();
	}

	/** 控件监听 */
	private void viewLinstener() {
		edit_phone.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (TextUtils.isEmpty(login_phone)) {
					edit_password.setText("");
				}
			}
		});

	}

	/** 登录验证 */
	protected void loginCheck() {
		login_phone = edit_phone.getText().toString().trim();
		login_password = edit_password.getText().toString().trim();
		encode = MD5Tool.MD5(login_password);
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", "" + JaderApplication.USER_LOGIN);
		params.addBodyParameter("login_phone", login_phone);
		params.addBodyParameter("password", encode);
		httpUtils = new HttpUtils();
		dialog = JaderApplication.getMyDialog(this);
		dialog.show();
		httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						JaderApplication.show(getBaseContext(), arg1.toString());
						if (dialog.isShowing()) {
							dialog.dismiss();
						}
					}

					@Override
					public void onSuccess(ResponseInfo<String> info) {
						String result = info.result;
						if ("null".equals(result)) {
							JaderApplication.show(getBaseContext(),
									"用户名或密码错误,请重新输入");
						} else {
							Gson gson = new Gson();
							User user = gson.fromJson(result, User.class);
							String userid = "" + user.getUserid();
							String username = user.getUsername();
							String password = user.getPassword();
							String user_pic = user.getPicture();
							String phone = user.getPhone();
							String gender = user.getGender();
							Log.e("share", userid + username + password + phone
									+ gender);
							// 缓存用户数据
							PreferenceUtil.getInstance(getBaseContext())
									.setShareUserId(userid);
							PreferenceUtil.getInstance(getBaseContext())
									.setShareUserName(username);
							PreferenceUtil.getInstance(getBaseContext())
									.setSharePassWord(password);
							PreferenceUtil.getInstance(getBaseContext())
									.setShareUserPic(user_pic);
							PreferenceUtil.getInstance(getBaseContext())
									.setShareUserPhone(phone);
							PreferenceUtil.getInstance(getBaseContext())
									.setShareUserGender(gender);
							PreferenceUtil.getInstance(getBaseContext())
									.setShareUserMoney(user.getMoney());
							// 显示登录成功，并跳转
							JaderApplication.user = user;
							finish();
							JaderApplication.show(getBaseContext(), "登录成功");
						}
						if (dialog.isShowing()) {
							dialog.dismiss();
						}
					}
				});

	}

	/** 初始化控件 */
	private void initView() {
		edit_phone = (EditText) findViewById(R.id.edit_login_phone);
		edit_password = (EditText) findViewById(R.id.edit_login_password);
		login = (Button) findViewById(R.id.btn_login);
		tv_forget = (TextView) findViewById(R.id.tv_login_forget);
		tv_register = (TextView) findViewById(R.id.tv_login_register);
		login_back = (ImageView) findViewById(R.id.top_actionbar_back);
		tv_login_title = (TextView) findViewById(R.id.top_actiobar_text);
		tv_login_title.setText("登录");
		// 单击事件
		login.setOnClickListener(this);
		tv_forget.setOnClickListener(this);
		tv_register.setOnClickListener(this);
		login_back.setOnClickListener(this);
	}

	/**
	 * 判断是否为空
	 * 
	 * @return 返回
	 */
	private boolean isNull() {
		if (TextUtils.isEmpty(login_phone) && TextUtils.isEmpty(login_password)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			if (isNull() == false) {
				Toast.makeText(LoginActivity.this, getString(R.string.isnull),
						Toast.LENGTH_SHORT).show();
			} else {
				loginCheck();
			}
			break;
		case R.id.tv_login_forget:
			Intent forget = new Intent(LoginActivity.this, ForgetActivity.class);
			startActivity(forget);
			break;
		case R.id.tv_login_register:
			Intent register = new Intent(LoginActivity.this,
					RegisterActivity.class);
			startActivity(register);
			break;
		case R.id.top_actionbar_back:
			Intent back = new Intent(this, SouYeFragment.class);
			startActivity(back);
			break;
		default:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		finish();
	}
}
